package com.xsy.demo

import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import tej.androidnetworktools.lib.Device
import tej.androidnetworktools.lib.Route
import tej.androidnetworktools.lib.scanner.NetworkScanner
import tej.androidnetworktools.lib.scanner.OnNetworkScanListener
import tej.androidnetworktools.lib.scanner.OnTracerouteListener
import tej.androidnetworktools.lib.scanner.Traceroute


class MainActivity : AppCompatActivity() {
    private val TAG = "devices"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        NetworkScanner.init(this)
        findViewById<Button>(R.id.btn).setOnClickListener {
            Traceroute.start("baidu.com", object : OnTracerouteListener {
                override fun onRouteAdd(route: Route) {
                    Log.d(
                        TAG, "traceroute: IP Address =>" + route.ipAddress + "=>"
                                + "RAW: " + route.rawAddress
                    )
                }

                override fun onComplete(routes: List<Route>) {
                    Log.d(TAG, "traceroute: " + "completed total: " + routes.size)
                }

                override fun onFailed() {
                    Log.d(TAG, "traceroute failed")
                }
            })
        }
        NetworkScanner.scan(object : OnNetworkScanListener {

            override fun onComplete(devices: MutableList<Device>?) {
                if (devices != null) {
                    for (device in devices) {
                        Log.d(
                            "device",
                            device.hostname + "\n" + device.vendorName + "\n" + device.macAddress
                        )
                    }
                }
            }

            override fun onFailed() {
                Log.d("device", "failed")
            }
        })

        Traceroute.init(this)

    }


}